﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class page_Plan_Manager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            bool IsUse = Shove._Convert.StrToBool(dr["IsUse"].ToString(), false);

            if (IsUse)
            {
                e.Item.Cells[3].Text = "正常";
                Button btnStart = (Button)e.Item.Cells[4].FindControl("btnStart");

                btnStart.Enabled = false;
            }
            else
            {
                e.Item.Cells[3].Text = "禁用";
                Button btnStop = (Button)e.Item.Cells[4].FindControl("btnStop");

                btnStop.Enabled = false;
            }
        }
    }

    protected void g_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        long ID = Shove._Convert.StrToLong(e.Item.Cells[5].Text, -1);
        DAL.Tables.tbl_CustomerPlan table = new DAL.Tables.tbl_CustomerPlan();

        if (e.CommandName == "btnEdit")
        {
            Response.Redirect("Plan_Edit.aspx?id="+ID);
        }
        else if (e.CommandName == "btnStart")   //启用
        {
            table.IsUse.Value = 1;

            table.Update("[ID]=" + ID);
        }
        else if (e.CommandName == "btnStop")    //停用
        {
            table.IsUse.Value = 0;

            table.Update("[ID]=" + ID);
        }

        BindData();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Plan_Edit.aspx");
    }

    private void BindData()
    {
        DataTable dt = new DAL.Tables.tbl_CustomerPlan().Open("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }
}
